package com.jnxx.robot.controller;

import com.jnxx.robot.mapper.RobotMapper;
import com.jnxx.robot.service.IRobotService;
import com.jnxx.robot.domain.Robot;
import com.jnxx.robot.query.RobotQuery;
import com.jnxx.basic.util.AjaxResult;
import com.jnxx.basic.query.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/robot")
public class RobotController {

    @Autowired
    public IRobotService robotService;

    @Autowired
    private RobotMapper robotMapper;

    /**
     * 接口：添加或修改
     * @param robot  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Robot robot){ 
        if( robot.getId()!=null){
		robotService.update(robot);
        }else{
                robotService.add(robot);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        robotService.del(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        robotService.batchDel(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Robot findOne(@PathVariable("id")Long id) {
        return robotService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Robot> findAll(){
        return robotService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/loadAll")
    public AjaxResult page(@RequestBody RobotQuery query) {
        PageInfo<Robot> page = robotService.page(query);
        return AjaxResult.success(page);
    }

    @PostMapping("/updateRobot")
    public AjaxResult updateRobot(@RequestBody Robot robot) {
        robotService.update(robot);
        return AjaxResult.success();
    }

    @PostMapping("/removeRobot")
    public AjaxResult removeRobot(@RequestBody Robot robot) {
        robotService.del(robot.getId());
        return AjaxResult.success();
    }

    @PostMapping("/addRobot")
    public AjaxResult addRobot(@RequestBody Robot robot) {
        robotMapper.add(robot);
        return AjaxResult.success();
    }
}
